package controllers

import dao._
import implicits.Implicits._
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}
import tool.{FormTool, Tool}
import utils.Utils

import java.io.File
import javax.inject.Inject
import scala.collection.immutable.SeqMap
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 11/5/2020
 */
class MutantController @Inject()(cc: ControllerComponents)(
  implicit val prokaryoteDao: ProkaryoteDao,
  implicit val bioSampleDao: BioSampleDao,
  implicit val assemblyDao: AssemblyDao,
  implicit val snpDao: SnpDao,
  implicit val cdsSnpDao: CdsSnpDao,
  implicit val cdsInDelDao: CdsInDelDao,
  implicit val interInDelDao: InterInDelDao,
) extends AbstractController(cc) {

  def toSnp = Action { implicit request =>
    Ok(views.html.mutant.snp())
  }

  def toInDel = Action { implicit request =>
    Ok(views.html.mutant.inDel())
  }

  def getAllSnpByServer= Action.async { implicit request =>
    val map=request.body.asJson.get.toJsObject.value
    val pageData = FormTool.pageForm.bindFromRequest().get
    snpDao.selectAll(map,pageData).map { case (size, x) =>
      val array = Utils.getJsonByTs(x)
      Ok(Json.obj("rows" -> array, "total" -> size))
    }
  }

  def getCdsSnpByServer= Action.async { implicit request =>
    val map=request.body.asJson.get.toJsObject.value
    val pageData = FormTool.pageForm.bindFromRequest().get
    cdsSnpDao.selectAll(map,pageData).map { case (size, x) =>
      val array = Utils.getJsonByTs(x)
      Ok(Json.obj("rows" -> array, "total" -> size))
    }
  }

  def getCdsInDelByServer= Action.async { implicit request =>
    val map=request.body.asJson.get.toJsObject.value
    val pageData = FormTool.pageForm.bindFromRequest().get
    cdsInDelDao.selectAll(map,pageData).map { case (size, x) =>
      val array = Utils.getJsonByTs(x)
      Ok(Json.obj("rows" -> array, "total" -> size))
    }
  }

  def getInterInDelByServer= Action.async { implicit request =>
    val map=request.body.asJson.get.toJsObject.value
    val pageData = FormTool.pageForm.bindFromRequest().get
    interInDelDao.selectAll(map,pageData).map { case (size, x) =>
      val array = Utils.getJsonByTs(x)
      Ok(Json.obj("rows" -> array, "total" -> size))
    }
  }

}
